package com.zhao.dota.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhao.dota.mapper.PlayerMapper;
import com.zhao.dota.model.DotaMatch;
import com.zhao.dota.model.Player;
import com.zhao.dota.processor.PlayerSummariesProcessor;
import com.zhao.dota.service.PlayerService;
import com.zhao.dota.util.SteamUtil;
import org.springframework.stereotype.Service;
import com.zhao.dota.exception.SteamHttpException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@Slf4j
public class PlayerServiceImpl extends ServiceImpl<PlayerMapper, Player> implements PlayerService {

    @Resource
    private PlayerSummariesProcessor playerSummariesProcessor;
/*    @Resource
    private RedisTemplate<String, Player> redisTemplate;*/

    @Override
    public Map<Long, Player> getOrCreatePlayers(DotaMatch match, List<Long> steamIds) {
        Map<Long, Player> result = new HashMap<>();
        List<PlayerSummariesProcessor.Player> players;
        try {
            players = playerSummariesProcessor.apply(steamIds);
        } catch (SteamHttpException e) {
            players = steamIds.stream().map(id -> {
                PlayerSummariesProcessor.Player p = new PlayerSummariesProcessor.Player();
                p.setSteamId(id);
                return p;
            }).collect(Collectors.toList());
        }

        for (PlayerSummariesProcessor.Player p : players) {
            Player player = getBySteamId(p.getSteamId());
            if (player == null) {
                player = new Player();
            }
            if (StringUtils.hasText(p.getPersonaName())) {
                player.setName(p.getPersonaName());
            }
            player.setSteamId(p.getSteamId());
            player.setAvatar(p.getAvatar());
            player.setAvatarFull(p.getAvatarFull());
            player.setRealName(p.getRealName());
            if (player.getLastUpdateTime() == null || player.getLastUpdateTime().isBefore(match.getEndTime())) {
                player.setLastUpdateTime(match.getEndTime());
            }
            player.setShortSteamId(SteamUtil.getShortSteamId(p.getSteamId()));
            saveOrUpdate(player);
            result.put(p.getSteamId(), player);
        }
        return result;
    }

    @Override
    public Page<Player> search(String search, Page<Player> page) {
        LambdaQueryWrapper<Player> query = Wrappers.lambdaQuery(Player.class)
                .like(Player::getShortSteamId, search).or().like(Player::getName, search);
        return page(page, query);
    }

    private Player getBySteamId(Long steamId) {
/*        String key = StringUtils.join(CacheConstant.PLAYER, "steamId", steamId, ":");
        Player player = redisTemplate.opsForValue().get(key);
        if (player != null) {
            return player;
        }*/
        LambdaQueryWrapper<Player> query = Wrappers.lambdaQuery(Player.class).eq(Player::getSteamId, steamId);
        Player one = getOne(query);
/*        if (one == null) {
            return null;
        }
        redisTemplate.opsForValue().set(key, one, Duration.ofDays(1));*/
        return one;
    }

}
